
// INCLUDES ********************************************************************

#include "TouchListener.h"

#include "TouchController.h"

#include "Timer.h"

#include "Logger.h"



// NAMESPACES ******************************************************************

using namespace Meteor;


// FORWARD DECLARATIONS ********************************************************

// *****************************************************************************

#pragma mark -
#pragma mark Constructor/Destructor
// -----------------------------------------------------------------------------

TouchController::TouchController() : mTouchStartTime( 0), mTouchEndTime( 0)
{
}

// -----------------------------------------------------------------------------

TouchController::~TouchController()
{
}


#pragma mark -
#pragma mark Touches
// -----------------------------------------------------------------------------

void TouchController::TouchBegan( const Vector2D& position)
{
    mTouchStartTime = Timer::GetCurrentTime();
    
    containertype::reverse_iterator iter = mListeners.rbegin();

    while ( iter != mListeners.rend())
    {
        ( *iter)->TouchBegan( position);
        
        if ( ( *iter)->SwallowsTouches() && ( *iter)->DidSwallowTouch())
        {
            break;
        }
        
        iter++;
    }
}

// -----------------------------------------------------------------------------

void TouchController::TouchMoved( const Vector2D& position)
{
    containertype::reverse_iterator iter = mListeners.rbegin();

    while ( iter != mListeners.rend())
    {
        ( *iter)->TouchMoved( position);
        
        if ( ( *iter)->SwallowsTouches() && ( *iter)->DidSwallowTouch())
        {
            break;
        }
        
        iter++;
    }
}

// -----------------------------------------------------------------------------

void TouchController::TouchEnded( const Vector2D& position)
{
    mTouchEndTime = Timer::GetCurrentTime();
    
    mTouchDuration = ( mTouchEndTime-mTouchStartTime);

    containertype::reverse_iterator iter = mListeners.rbegin();

    while ( iter != mListeners.rend())
    {
        ( *iter)->TouchEnded( position, mTouchDuration);
        
        if ( ( *iter)->SwallowsTouches() && ( *iter)->DidSwallowTouch())
        {
            break;
        }
        
        iter++;
    }
}


#pragma mark -
#pragma mark Delegates
// -----------------------------------------------------------------------------

void TouchController::AddTouchListener( TouchListener* listener)
{
    containertype::iterator iter = mListeners.find( listener);

    if ( iter != mListeners.end())
    {
        Logger::DebugLog( "TouchController::addDelegate - \
delegate already added.");
        
        return;
    }

    mListeners.insert( listener);
}

// -----------------------------------------------------------------------------

void TouchController::RemoveTouchListener( TouchListener* listener)
{
    containertype::iterator iter = mListeners.find( listener);
 
    if ( iter != mListeners.end())
    {
        mListeners.erase( iter);
    } 
}
